package com.ruoyi.web.controller.system;

import com.ruoyi.business.domain.BusPerson;
import com.ruoyi.business.service.IBusPersonService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;

import javax.annotation.Resource;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Resource
    private ISysConfigService configService;

    @Resource
    private IBusPersonService busPersonService;

    @Transactional
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }

        if (StringUtils.isEmpty(user.getPassword()))
        {
            return error("密码不能为空");
        }

        String msg = registerService.register(user);

        //        添加进员工表
        BusPerson person = new BusPerson();
        person.setPersonName(user.getUsername());
        person.setPersonPwd(SecurityUtils.encryptPassword(user.getPassword()));
        busPersonService.register(person);

        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
}
